package com.gpsnav.file;
    
import java.util.Vector;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
//import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import com.gpsnav.bo.Options;

final class FileExplorerListFieldImpl extends ListField implements ListFieldCallback {
    private Vector _elements = new Vector();

    FileExplorerListFieldImpl() { setCallback(this); }
    FileExplorerListFieldImpl(int numRows) { 
        super(numRows);
        setCallback(this);
    }

    public void add(Object element) {
        _elements.addElement(element);
        setSize(getSize());
    }

//    protected void layout(int width, int height) {
//        super (width, height-30);                  
//    }
    
    // see JavaDocs for ListFieldCallback.drawListRow()
    public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) {
        if (index < getSize()) {
            FileExplorerFileHolder fileholder = (FileExplorerFileHolder)_elements.elementAt(index);
            String text;
            if (fileholder.isDirectory()) text = fileholder.getPath();
            else text = fileholder.getFileName();
            graphics.drawText(text, 0, y);
        }
    }

    
    // see JavaDocs for ListFieldCallback.get()
    public Object get(ListField listField, int index) {
        if (index >= 0 && index < getSize())return _elements.elementAt(index);
        return null;
    }

    
    // see JavaDocs for ListFieldCallback.getPreferredWidth()
//dcc    public int getPreferredWidth(ListField listField) { return Display.getWidth(); }
    public int getPreferredWidth(ListField listField) { return 200; }

    
    // see JavaDocs for ListFieldCallback.indexOfList()
    public int indexOfList(ListField listField, String prefix, int start) { return listField.indexOfList(prefix,start); }

    public boolean keyChar(char key, int status, int time){
        if (getSize() > 0 && key == Characters.SPACE) {
            getScreen().scroll(Manager.DOWNWARD);
            return true;
        }

        return super.keyChar(key, status, time);
    }

    public int getSize() { return (_elements != null) ? _elements.size() : 0; }

    public void removeAll() { _elements.removeAllElements(); setSize(0); }

    protected void paintBackground(Graphics g) {
        if (Options.getNightMode()) {
            g.setBackgroundColor(Color.BLACK);
            g.setColor(Color.RED);
        } else {
            g.setBackgroundColor(Color.WHITE);
            g.setColor(Color.BLACK);
        }
        g.clear();
    }

}
